package com.bootdo.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.system.domain.AdvertisingDO;
import com.bootdo.system.domain.DealSlideshowDO;
import com.bootdo.system.domain.ManageDO;
import com.bootdo.system.domain.MiniappDetailsDO;
import com.bootdo.system.service.CollectionService;
import com.bootdo.system.service.DealSlideshowService;
import com.bootdo.system.service.MiniappDetailsService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.oa.domain.NotifyDO;
import com.bootdo.oa.service.NotifyService;

/**
 * 
 * @ClassName: MiniappDetailsController  
 * @Description: TODO(移动端小程序)  
 * @author lzy  
 * @date 2018年6月6日  
 *
 */
 
@Controller
@RequestMapping("/system/miniappDetails")
public class MiniappDetailsController {
	@Autowired
	private MiniappDetailsService miniappDetailsService;
	
	@Autowired
	private DealSlideshowService dealSlideshowService;
	
	@Autowired
	CollectionService  collectionService;
	
	@Autowired
	NotifyService notifyService;
	
	@Autowired
	private BootdoConfig bootdoConfig;
	
	@GetMapping()
	@RequiresPermissions("system:miniappDetails:miniappDetails")
	String MiniappDetails(){
	    return "system/miniappDetails/miniappDetails";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:miniappDetails:miniappDetails")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MiniappDetailsDO> miniappDetailsList = miniappDetailsService.list(query);
		int total = miniappDetailsService.count(query);
		PageUtils pageUtils = new PageUtils(miniappDetailsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:miniappDetails:add")
	String add(){
	    return "system/miniappDetails/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:miniappDetails:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		MiniappDetailsDO miniappDetails = miniappDetailsService.get(id);
		model.addAttribute("miniappDetails", miniappDetails);
	    return "system/miniappDetails/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestParam("file1") MultipartFile file1,@RequestParam("file2") MultipartFile file2,@RequestParam("file3") MultipartFile file3,
			@RequestParam("file4") MultipartFile file4,@RequestParam("file5") MultipartFile file5,Long userId,MiniappDetailsDO miniappDetails){
		if(userId == null){
			return R.error("账号未登陆！");
		}
		if(file1.isEmpty()){
			return R.error("第一张图片未上传");
		}
		if(file2.isEmpty()){
			return R.error("第二张图片未上传！");
		}
		if(file3.isEmpty()){
			return R.error("第三张图片未上传！");
		}
		if(file4.isEmpty()){
			return R.error("第四张图片未上传！");
		}
		if(file5.isEmpty()){
			return R.error("第五张图片未上传！");
		}
		//主图片
		String url1 = null;
		//商品详情图片
		String url2 = null;
		String url3 = null;
		String url4 = null;
		String url5 = null;
		try {
			url1 = OssUtil.fileUpload(file1);
			url2 = OssUtil.fileUpload(file2);
			url3 = OssUtil.fileUpload(file3);
			url4 = OssUtil.fileUpload(file4);
			url5 = OssUtil.fileUpload(file5);
		} catch (Exception e) {
			return R.error("上传出现故障，请稍后再试");
		}
		miniappDetails.setMiniappMainImgurl(url1);
		miniappDetails.setMiniappDetailsImgurl1(url2);
		miniappDetails.setMiniappDetailsImgurl2(url3);
		miniappDetails.setMiniappDetailsImgurl3(url4);
		miniappDetails.setMiniappDetailsImgurl4(url5);
		miniappDetails.setStatus(0);
		if(miniappDetailsService.save(miniappDetails)>0){
			//发通知
			Long[] userid = {userId};
			NotifyDO notifyDO = new NotifyDO();
			notifyDO.setType("1");
			notifyDO.setStatus("1");
			notifyDO.setCreateDate(new Date());
			notifyDO.setCreateBy(Constant.SQL_NUMBER);
			notifyDO.setTitle("发布通知！");
			notifyDO.setUserIds(userid);
			notifyDO.setContent("您的发布正在审核中，请耐心等候！");
			notifyService.save(notifyDO);
			return R.ok("发布成功！请等待审核通过");
		}else {
			return R.error("网络出现故障，请稍后再试");
		}
		
	}
	
	/**
	 * 查询所有状态为1的小程序对象(即查所有已经审核通过的小程序)
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getListByStatus")
	public List<MiniappDetailsDO> getListByStatus(){
		List<MiniappDetailsDO> listByStatus = miniappDetailsService.getListByStatus();
		return listByStatus;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:miniappDetails:edit")
	public R update(@RequestParam("file1") MultipartFile file1,@RequestParam("file2") MultipartFile file2,MiniappDetailsDO miniappDetails){
		String fileName1 = file1.getOriginalFilename();
		String fileName2 = file2.getOriginalFilename();
		fileName1 = FileUtil.renameToUUID(fileName1);
		fileName2 = FileUtil.renameToUUID(fileName2);
		try {
			FileUtil.uploadFile(file1.getBytes(), bootdoConfig.getUploadPath(), fileName1);
			FileUtil.uploadFile(file2.getBytes(), bootdoConfig.getUploadPath(), fileName2);
		} catch (Exception e) {
			return R.error();
		}
		String url1 = bootdoConfig.getUploadPath()
			+ miniappDetailsService.get(miniappDetails.getId()).getMiniappDetailsImgurl1().replace("" + bootdoConfig.getHttpPath() + "/files/", "");// 服务端
		String url2 = bootdoConfig.getUploadPath()
				+ miniappDetailsService.get(miniappDetails.getId()).getMiniappMainImgurl().replace("" + bootdoConfig.getHttpPath() + "/files/", "");// 服务端
		//String url= bootdoConfig.getUploadPath() + companyService.get(company.getId()).getCompanyUrl().replace("/files/", ""); //本地
		boolean b1 = FileUtil.deleteFile(url1);
		boolean b2 = FileUtil.deleteFile(url2);
		if (b1 & b2) {
			miniappDetails.setMiniappDetailsImgurl1(bootdoConfig.getHttpPath() + "/files/" + fileName1); // 服务端
			miniappDetails.setMiniappMainImgurl(bootdoConfig.getHttpPath() + "/files/" + fileName2);// 服务端
			
			miniappDetailsService.update(miniappDetails);
		} else {
			return R.error("修改失败");
		}

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:miniappDetails:remove")
	public R remove( Integer id){
		String url1 = miniappDetailsService.get(id).getMiniappMainImgurl().substring(45, 62);
		String url2 = miniappDetailsService.get(id).getMiniappDetailsImgurl1().substring(45, 62);
		String url3 = miniappDetailsService.get(id).getMiniappDetailsImgurl2().substring(45, 62);
		String url4 = miniappDetailsService.get(id).getMiniappDetailsImgurl3().substring(45, 62);
		String url5 = miniappDetailsService.get(id).getMiniappDetailsImgurl4().substring(45, 62);
		if(miniappDetailsService.remove(id)>0){
			OssUtil.deleteBucketInfo(url1);
			OssUtil.deleteBucketInfo(url2);
			OssUtil.deleteBucketInfo(url3);
			OssUtil.deleteBucketInfo(url4);
			OssUtil.deleteBucketInfo(url5);
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:miniappDetails:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		miniappDetailsService.batchRemove(ids);
		return R.ok();
	}
	
	@ResponseBody
	@GetMapping("/audit")
	R audit(@RequestParam Integer id,@RequestParam Integer status,Model model){
		if(status==1){
			MiniappDetailsDO miniappDetailsDO = miniappDetailsService.get(id);
			miniappDetailsDO.setStatus(1);;
			miniappDetailsService.update(miniappDetailsDO);
			//发通知
			Long[] userid = {miniappDetailsDO.getUserId().longValue()};
			NotifyDO notifyDO = new NotifyDO();
			notifyDO.setType("1");
			notifyDO.setStatus("1");
			notifyDO.setCreateDate(new Date());
			notifyDO.setCreateBy(Constant.SQL_NUMBER);
			notifyDO.setTitle("审核通知！");
			notifyDO.setUserIds(userid);
			notifyDO.setContent("恭喜您，您的发布审核通过！");
			notifyService.save(notifyDO);
			return R.ok("操作成功");
		}else{
			MiniappDetailsDO miniappDetailsDO = miniappDetailsService.get(id);
			miniappDetailsDO.setStatus(2);
			miniappDetailsService.update(miniappDetailsDO);
			//发通知
			Long[] userid = {miniappDetailsDO.getUserId().longValue()};
			NotifyDO notifyDO = new NotifyDO();
			notifyDO.setType("1");
			notifyDO.setStatus("1");
			notifyDO.setCreateDate(new Date());
			notifyDO.setCreateBy(Constant.SQL_NUMBER);
			notifyDO.setTitle("审核通知！");
			notifyDO.setUserIds(userid);
			notifyDO.setContent("抱歉，您的审核未通过，请联系客服！");
			notifyService.save(notifyDO);
			return R.ok("操作成功");
		}
	}
	
	/**
	 * 小程序详情页面
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/detailsPage")
	public Map<String, Object> detailsPage(@RequestParam("appId") Integer appId) {
		MiniappDetailsDO miniappDetails = miniappDetailsService.get(appId);
		Map<String, Object> map = new HashMap<>();
		map.put("userId", appId);
		List<DealSlideshowDO> list = dealSlideshowService.list(map);
		Map<String, Object> allmap = new HashMap<>();
		allmap.put("miniappDetails", miniappDetails);
		allmap.put("piclist", list);

		return allmap;

	}
}
